package barcode.android.internet.impl;

import junit.framework.Assert;
import android.test.ActivityInstrumentationTestCase2;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.UserInfoDao;
import barcode.android.dao.impl.BaseDaoImpl;
import barcode.android.dao.impl.SqliteDatabaseState;
import barcode.android.dao.impl.UserInfoDaoImpl;
import barcode.android.internet.AccountInternet;
import barcode.android.internet.UserInternet;
import barcode.android.model.UserInfo;

public class UserInternetTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {
	private AccountInternet anet = new AccountInternetImpl();
	private UserInternet net = new UserInternetImpl();
	private UserInfoDao dao = new UserInfoDaoImpl();

	public UserInternetTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testPostUserInfo() throws Exception {
		InternetResult res = anet.login("U200810001", "123456");
		Assert.assertEquals(ResultType.Success, res.getType());

		UserInfo info = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			info = dao.query();
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		res = net.postUserInfo((String) res.getValue(), info.getEmail(), info
				.getQq(), info.getPhone(), info.getAbstracts(), info.getDorm()
				.getName());
		Assert.assertEquals(ResultType.Success, res.getType());
	}

	public void testGetOrganizations() throws Exception {
		InternetResult res = anet.login("U200810001", "123456");
		Assert.assertEquals(ResultType.Success, res.getType());

		res = net.getOrganizations("U200810001", (String) res.getValue());
		Assert.assertEquals(ResultType.Success, res.getType());
	}

	public void testGetActivities() throws Exception {
		InternetResult res = net.getUserActs("U200810001");
		Assert.assertEquals(ResultType.Success, res.getType());
	}

	public void testGetUserInfo() throws Exception {
		InternetResult res = anet.login("U200810001", "123456");
		Assert.assertEquals(ResultType.Success, res.getType());

		res = net.getUserInfo("U200810001", (String) res.getValue());
		Assert.assertEquals(ResultType.Success, res.getType());
	}

	public void testPutUserInfo() throws Exception {
		InternetResult res = anet.login("U200810001", "123456");
		Assert.assertEquals(ResultType.Success, res.getType());

		UserInfo info = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
			info = dao.query();
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		res = net.putUserInfo("U200810001", (String) res.getValue(),
				info.getEmail(), info.getQq(), info.getPhone(),
				info.getAbstracts(), info.getDorm().getName());
		Assert.assertEquals(ResultType.Success, res.getType());
	}
}
